<?php
class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library(array(
            'form_validation',
            'session'
        ));
        $this->load->helper(array(
            'url'
        ));
        $this->load->model(array(
            'login_model'
        ));
    }
    
    public function index() {
        $session = $this->session->all_userdata();
        if(!empty($session['SID'])) {
            redirect('/trofeos/', 'refresh');
        }
        
        $this->form_validation->set_rules('email', 'email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $usuario = $this->login_model->login($this->input->post('email'), $this->input->post('password'));
            if(!empty($usuario)) {
                $datos = array(
                    'SID' => $usuario['idusuarios'],
                    'nombre' => $usuario['nombre']
                );
                $this->session->set_userdata($datos);
                redirect('/trofeos/', 'refresh');
            }
        }
        $this->load->view('login/index');
    }
}
?>
